package com.tuxianchao.springboottask.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * created by
 *
 * @author tuxianchao
 * @date 2018/5/2 下午5:35
 * 定时任务多线程执行配置
 */
@Configuration
@EnableAsync
public class TaskAsyncConfig {
    /*
            最好是冲配置文件读取
            @Value注解注入
    */
    private int corePoolSize = 5;
    private int maxPoolSize = 100;
    private int queueCapacity = 5;

    /**
     * 定时任务线程池配置
     *
     * @return
     */
    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}
